#include "PanelItem.h"
#include "AbstractManager.h"
#include "GraphicsText.h"
#include "vmLibrary.h"
#include "AbstractPanel.h"

#include <QFont>
#include <QPen>
#include <QPainter>

PanelItem::PanelItem(std::string name, AbstractManager* manager, AbstractPanel* panel)
	: AbstractShapeItem(name, manager, panel)
{
	this->_isRemovable = false;
	this->_panel = panel;
	this->_graphicsTextName = NULL;
	this->_parent = NULL;
	this->_fontColor = new QPen(QColor(DEFAULT_FONT_COLOR));
	this->_backgroundColor = new QBrush(QColor(DEFAULT_BACKGROUND_COLOR));
	this->_width = 154;
	initName();
}

PanelItem::~PanelItem()
{

}

void PanelItem::initName()
{
	std::string name = _name;
	
	QFont sansFont("Helvetica", 9);
	sansFont.setBold(true);

	_graphicsTextName = new GraphicsText(name.c_str(), this);
	_graphicsTextName->setFont(sansFont);
	_graphicsTextName->setDefaultTextColor(_fontColor->color());
	_graphicsTextName->setAlignment(Qt::AlignRight);
	_graphicsTextName->setX( this->_width - RIGHT_PADDING - _graphicsTextName->boundingRect().width() );
	_graphicsTextName->setY( 0 );

	setName(name);
}

void PanelItem::setName( std::string name )
{
	_name = name;
	_graphicsTextName->setPlainText(QString(name.c_str()));
}

// 設定 Item 名稱是否可以被修改
void PanelItem::setNameEditable( bool editable )
{
	_graphicsTextName->setEditable(editable);
}

void PanelItem::setRemovable( bool isRemovable )
{
	this->_isRemovable = isRemovable;
}

void PanelItem::paint( QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget )
{

	// 畫 Item 背景顏色與分隔線
	if ( this->isSelected() ) {
		painter->setBrush(QColor("#4B95E5"));
	} else {
		painter->setBrush(*_backgroundColor);
	}

	painter->setPen(Qt::NoPen);
	painter->drawRect( 0, 1, this->_width, HEIGHT );
	painter->setPen(QPen(QColor("#AAAAAA")));
	painter->drawLine( 0, HEIGHT, this->_width, HEIGHT);

	// 設定 Item 名稱顏色
	if ( this->isSelected() ) {
		_graphicsTextName->setDefaultTextColor(Qt::white);
	} else {
		_graphicsTextName->setDefaultTextColor(_fontColor->color());
	}

	// 畫 Item type block 跟 type 名稱
	drawType(painter);
}


QRectF PanelItem::boundingRect() const
{
	return QRectF( 0, 0, this->_width, HEIGHT );
}

void PanelItem::remove()
{
	if ( this->_isRemovable) {
		_panel->deleteItem(this);
	} else {
		this->setSelected(false); // 把選取狀態設定成 false，不然會造成無窮迴圈
		// (因為當按下delete時，DiagrameScene會呼叫所有被選取item的remove())
	}

}



void PanelItem::setParent( PanelItem* parent )
{
	this->_parent = parent;
	QObject::setParent(parent);
}

PanelItem* PanelItem::getParent()
{
	return _parent;
}

void PanelItem::drawType( QPainter * painter )
{
	QFont sansFont("Helvetica", 8);
	QFontMetrics fontMetrics(sansFont);
	int typeBlockWidth = fontMetrics.width(_type.c_str()) + TYPE_BLOCK_LEFT_PADDING + TYPE_BLOCK_RIGHT_PADDING;
	int typeBlockHeight = fontMetrics.height();

	painter->setBrush(QBrush(QColor("#575757")));
	painter->drawRoundedRect( LEFT_PADDING , TOP_PADDING, typeBlockWidth, typeBlockHeight, 2 ,2);

	painter->setPen(QPen(QColor("#FFFFFF")));
	painter->setFont(sansFont);
	painter->drawText( LEFT_PADDING, TOP_PADDING, typeBlockWidth, typeBlockHeight, Qt::AlignCenter, trf(_type).c_str());
}
